﻿/*
 * OverviewManager
 * 
 * Copyright (c) Adam Sida, asida@centrum.cz
 * 
 * All rights reserved.
 */
using System;
using System.Windows;
using System.Collections.Generic;
using Petra.Gis.Geometry.Support;
using Petra.Gis.Imaging.Tiff;



namespace Petra.Gis.Geometry.Image

{
  /// <summary>
  /// provides GIS specific functionality for raster images display
  /// to accelerate extra large image rendering this manager does this:
  /// 1. for tiff image scans for overviews, if found uses them
  /// 2. for other images, prepares overview bitmaps on disk as files
  /// and creates special configuration file with map of these overviews
  /// </summary>
  public class OverviewManager
  {
    private string _workFolderPath;
    private long _maxfolderByteSize;

    public OverviewManager()
    {

    }




    public OverviewManager(string workFolderPath, long cacheByteSize)
    {
      _workFolderPath = workFolderPath;
      _maxfolderByteSize = cacheByteSize;
    }




    /// <summary>
    /// clear all files in workFolder
    /// </summary>
    public void ClearCache()
    {

    }




    /// <summary>
    /// main function of OverviewManager. Lookup for requested image type 
    /// if image is not with overviews then lookup in cache.
    /// if not found then creates new cache entry for it, if necessary prepares overviews
    /// and then return image frame blocks
    /// function doesn't do SRS transformations and doesn't use SRS info
    /// threfore user must support input values in native image geometry SRS and coordinate units
    /// </summary>
    /// <param name="image">valid image geometry</param>
    /// <param name="srcSrsClipArea">world coordinates of map clipping area in current image spatial reference system coordinates</param>
    /// <returns></returns>
    public List<ImageBlock> ReadGeometry(Grid image, BoundingBox srcSrsClipArea, Size mapImageResolution)
    {
      throw new NotImplementedException();

      //read geometry blob and resolve image file name
      SpatialReferencedImageData decoder = new SpatialReferencedImageData(image.DataLocation);


      //resolve pixel size
      double worldMapPixelSize = srcSrsClipArea.Width / mapImageResolution.Width;


      //try if image is with overviews
      if (decoder.IsImageWithOverviews)
      {
        double currentFrameWPS;
        //first image is always largest one, with smallest possible pixel
        Overview lastFrame = decoder.Overviews[0];
        foreach (Overview f in decoder.Overviews)
        {
          currentFrameWPS = image.BoundingBox.Width / f.pixelWidth;
          if (currentFrameWPS > worldMapPixelSize)
            break;
          else
            lastFrame = f;
        }
      }
      else //no overviews, try cache
      {
        if (decoder.Overviews[0].pixelWidth > 5000 || decoder.Overviews[0].pixelHeight > 5000)
        {
          //large image lookup managers cache
          if (IsImageInCache(image.DataLocation))
            //is in cache, just return it
            return LoadCachedImage(image.DataLocation);
          else
            //create new cache entry and load appropriate frame now
            return CreateCachedImage(image, srcSrsClipArea, mapImageResolution);
        }
        else //small image load directly
        {
          return decoder.Overviews[0].Blocks;
        }
      }

    }





    public bool IsImageInCache(string fileName)
    {
      return true;
    }




    public List<ImageBlock> LoadCachedImage(string fileName)
    {
      //image in cache must be newer than source image
      //otherwise must drop in cache and call CreateCachedImage
      throw new NotImplementedException();

    }





    public List<ImageBlock> CreateCachedImage(Grid image, BoundingBox srcSrsClipArea, Size mapImageResolution)
    {
      throw new NotImplementedException();
    }

  }
 
}
